package org.logtime.blog.module;

import java.util.List;
import org.logtime.blog.exception.ApiException;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

/**
 * @version: 1.0
 * @Author: itmins
 * @Email: itmins@gmail.com
 */
public class JsonModule {

	/**
	 * Return data
	 */
	private Object data;
	/**
	 * Return message
	 */
	private String message;
	/**
	 * Is success
	 */
	private boolean success;
	/**
	 * If error,errorCode
	 */
	private Long errorCode;

	public JsonModule(boolean success, Long errorCode, String message,
			Object object) {
		this.success = success;
		this.message = message;
		this.errorCode = errorCode;
		this.data = this.getJSONObjectString(object);
	}

	public String toString() {
		return this.getJSONObjectString(this);
	}

	@SuppressWarnings("rawtypes")
	private String getJSONObjectString(Object object) {
		if (null == object) {
			return null;
		}
		Object result = null;
		if (object instanceof List) {
			JSONArray jsonArray = new JSONArray();
			for (Object o : (List) object) {
				jsonArray.put(new JSONObject(o));
			}
			result = jsonArray;
		} else if (object instanceof PageModule) {
			result = new JSONObject(object);
			List data = ((PageModule) object).getData();
			if (null != data) {
				JSONArray jsonArray = new JSONArray();
				for (Object o : data) {
					jsonArray.put(new JSONObject(o));
				}
				try {
					((JSONObject) result).put("data", jsonArray);
				} catch (JSONException e) {
					throw new RuntimeException(e);
				}
			}
		} else {
			result = new JSONObject(object);
		}
		if (result instanceof JSONObject) {
			if (((JSONObject) result).has("class")) {
				((JSONObject) result).remove("class");
			}
		}
		return result.toString();
	}

	public static JsonModule getJson(Object object) {
		return new JsonModule(true, null, null, object);
	}

	public static JsonModule getJson(Exception e) {
		if (!(e instanceof ApiException)) {
			e = new ApiException(e);
		}
		return new JsonModule(false, ((ApiException) e).getErrorCode(),
				e.getMessage(), null);
	}

	public static JsonModule getJson(Long errorCode) {
		return new JsonModule(false, errorCode, null, null);
	}

	public static JsonModule getJson(Long errorCode, String message) {
		return new JsonModule(false, errorCode, message, null);
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Long getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(Long errorCode) {
		this.errorCode = errorCode;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
}